//
//  ProgressViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2019/1/3.
//  Copyright © 2019年 DUONIU_MAC. All rights reserved.
//

import UIKit

class ProgressViewController: KMViewController {
    
    fileprivate let scrollView = UIScrollView()
    fileprivate let headView = ProgressQueryView()
    fileprivate let bankView = ProgressBankView()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        self.navigationController?.navigationBar.shadowImage = nil
        
    }
    
    init(title:String) {
        super.init(nibName: nil, bundle: nil)
        
        self.title = title
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addSubView()

    }
    
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        scrollView.showsVerticalScrollIndicator = false
        scrollView.bounces = false
        self.view.addSubview(scrollView)
        
        headView.delegate = self
        headView.title = self.title
        scrollView.addSubview(headView)
        
        bankView.delegate = self
        bankView.title = self.title
        scrollView.addSubview(bankView)
        
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        headView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 300))
        }
        
        bankView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalTo(self.view)
            make.height.equalTo(0)
        }
    }

}

extension ProgressViewController : ProgressBankViewDelegate,ProgressQueryViewDelegate {
    
    //MARK: 选择银行
    func selectBank(urlStr: String) {
        
        let webVC = WebDetailsViewController.init(urlString: urlStr, title: nil)
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    //MARK: 选择热门银行
    func selectpopularBank(urlStr: String) {
        
        let webVC = WebDetailsViewController.init(urlString: urlStr, title: nil)
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    //MARK: 修改坐标
    func changeFrame(count: Int) {
        
        var sum = count/3
        if count%3 != 0 {
            sum += 1
        }
        
        bankView.snp.remakeConstraints { (make) in
        make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalTo(self.view)
            make.height.equalTo(CGFloat(sum) * RATIO_H(maxNum: 60))
        }
        
        self.view.layoutIfNeeded()
        
        scrollView.contentSize = .init(width: ScreenW, height: bankView.frame.maxY)
    }
    
}
